<template>
  <div class="package-deliver-merchant">
    <c-title :hide="false" text="招商员申请"></c-title>
    <template v-if="infoData.type == 0 && !infoData.apply_wait">
      <div class="cellBox">
        <van-cell-group>
          <van-cell title-style="flex:5.5rem 0;text-align: left;" value-class="valRigth" title="真实姓名">
            <template #default>
              <input type="text" class="inpStyle" placeholder="请输入真实姓名" v-model="realname" />
            </template>
          </van-cell>
          <van-cell title-style="flex:5.5rem 0;text-align: left;" value-class="valRigth" title="手机号">
            <template #default>
              <input type="text" class="inpStyle" placeholder="请输入手机号" v-model="mobile" />
            </template>
          </van-cell>
        </van-cell-group>
      </div>
      <div class="btn set-pc-style">
        <div class="submitBtn" @click="applySubmit">申请</div>
      </div>
    </template>
    <template v-if="infoData.apply_wait">
      <div class="statusBox">
        <div class="image">
          <img :src="imgWaiting" alt="" />
        </div>
        <span class="tips">申请已提交，请等待审核</span>
        <div class="statusBtn" @click="confirmBtn">去商城逛逛</div>
      </div>
    </template>
    <template v-if="infoData.type == 1 && !infoData.apply_wait">
      <div class="goodBox">
        <div class="title">购买<span class="redTxt">指定商品</span>之一升级成为招商员</div>
        <div class="list">
          <div class="lis" v-for="(item, index) in goodsList" :key="index" @click="toGood(item.id)">
            <div class="image">
              <img :src="item.thumb" alt="" />
            </div>
            <div class="contenBox">
              <div class="shopTitle van-ellipsis">{{ item.title }}</div>
              <div class="price">￥{{ item.price }}</div>
            </div>
          </div>
        </div>
      </div>
    </template>
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      infoData: {},
      realname: "",
      mobile: "",
      imgWaiting: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FagentShop%2F%E7%AD%89%E5%BE%85%E5%AE%A1%E6%A0%B8%402x.png",

      goodsList: [],
      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  activated() {
    this.page = 1;
    this.isLoadMore = true;
    this.total_page = 0;
    this.getData();
  },
  methods: {
    toGood(id) {
      this.$router.push(this.fun.getUrl("goods", { id: id }, {}));
    },
    confirmBtn() {
      this.$router.push(this.fun.getUrl("home", {}, {}));
    },
    async applySubmit() {
      if (this.fun.isTextEmpty(this.realname)) {
        this.$toast("请输入真实姓名");
        return;
      }
      if (this.fun.isTextEmpty(this.mobile)) {
        this.$toast("请输入手机号");
        return;
      }
      let { data, msg, result } = await $http.post("plugin.package-deliver-merchant.frontend.apply.apply", { realname: this.realname, mobile: this.mobile }, "");
      this.$toast(msg);
      if (result) {
        this.getData();
      }
    },
    async getData() {
      let { data, msg, result } = await $http.post("plugin.package-deliver-merchant.frontend.apply.index", {}, "");
      if (!result) return this.$toast(msg);
      this.infoData = data;
      this.goodsList = data.goods.data;
      this.isLoadMore = true;
      this.total_page = data.goods.last_page;
    },
    async getMoreData() {
      if (!this.isLoadMore) return;
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page += 1;
        let json = {
          page: this.page
        };
        let { data, msg, result } = await $http.post("plugin.package-deliver-merchant.frontend.apply.index", json, "");
        this.isLoadMore = true;
        if (!result) {
          this.$toast(msg);
          this.page = this.page - 1;
          this.isLoadMore = false;
        } else {
          this.goodsList = this.goodsList.concat(data.goods.data);
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.package-deliver-merchant {
  overflow: hidden;
  .goodBox {
    margin: 0.97rem 0.75rem;
    .title {
      font-weight: bold;
      font-size: 0.94rem;
      text-align: left;
      margin-bottom: 0.84rem;
      .redTxt {
        color: #f15353;
      }
    }
    .list {
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-wrap: wrap;
      .lis {
        background: #ffffff;
        border-radius: 0.75rem;
        display: flex;
        flex-direction: column;
        margin-bottom: 0.63rem;
        overflow: hidden;
        width: 10.66rem;
        .image {
          width: 10.66rem;
          height: 10.66rem;
          img {
            width: 100%;
            height: 100%;
          }
        }
        .contenBox {
          overflow: hidden;
          padding-left: 0.75rem;
          padding-right: 0.53rem;
          box-sizing: border-box;
          text-align: left;
          padding-top: 0.63rem;
          padding-bottom: 0.94rem;
          .shopTitle {
            font-weight: bold;
            font-size: 0.81rem;
          }
          .price {
            font-weight: 500;
            font-size: 0.94rem;
            color: #f15353;
            line-height: 1rem;
            margin-top: 0.84rem;
          }
        }
      }
    }
  }
  .cellBox {
    margin: 0.63rem 0.75rem;
    .van-cell-group {
      border-radius: 0.5rem;
    }
    .valRigth {
      text-align: left;
    }
    .inpStyle {
      border: none;
      width: 90%;
    }
  }
  .btn {
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 0.63rem 0.75rem;
    background: #fff;
    width: 100%;
    .submitBtn {
      background: #f15353;
      border-radius: 1.88rem;
      text-align: center;
      line-height: 2.5rem;
      color: #fff;
    }
  }
}
.statusBox {
  width: 21.94rem;
  height: 16.78rem;
  background: #ffffff;
  border-radius: 0.5rem 0.5rem 0.5rem 0.5rem;
  margin: 0.63rem auto;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  .image {
    width: 6.66rem;
    height: 6.66rem;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .tips {
    font-size: 0.88rem;
    color: #00001c;
    text-align: center;
    margin: 0.72rem 0 1.88rem 0;
  }
  .statusBtn {
    width: 6.69rem;
    height: 2.5rem;
    background: #f15353;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 1.25rem;
    font-weight: 500;
    font-size: 0.94rem;
    color: #ffffff;
  }
}
</style>
